$(function(){
	
	var _index=0,amzCommissionRate,txtCost,txtPrice,txtLength,txtWidth,txtHeight,txtWeightAcutal,txtFreightCost,txtRate,txtInRMB="",txtMLL="";

	var amzMarket="美国亚马逊";
	
	function init(){
			/*if($.cookie("stratTime") == null){
					var stratTime = new Date();
					var endTime = new Date();
					$.cookie("stratTime",stratTime);
					
					/*时间到了有效期前五分钟弹出提示框 
					endTime.setHours(endTime.getHours()+12);
					endTime.setMinutes(endTime.getMinutes()-5);
					
					var _time = endTime.getTime() - stratTime.getTime();
					setTimeout(function(){
						alert(1)
					},_time);
					
					/*有效期到了就清楚cookie
					endTime.setHours(endTime.getHours()+12);
					endTime.setMinutes(endTime.getMinutes()+5);
					var _times = endTime.getTime() - stratTime.getTime();
					setTimeout(function(){
						var _index = $.cookie("_index");
						for(var i=1;i<=_index;i++){
							$.cookie("stratTime",null);
							$.cookie('submitResult'+_index,null);
							$.cookie("modalValue"+_index,null);
							$.cookie("amzMarketTxt"+_index,null);
						}
					},_times)
					
			}*/
			
			if($.cookie('_index') == null){
				 _index = 0;
			}else{
				cookieF();
			}
	}

	init();

	function cookieF(){
		var cookieIndex = $.cookie("_index");
		for(var i=1;i<=cookieIndex;i++){
			addTr(i);
		}
	}
	
	$(".inputBtn").on("keyup",function(){
		if(inputEmpty() == true){
			$(".stratCountBtn").css({"cursor":"pointer","opacity":"1"}).addClass("countBtn stratHover");
		}else{
			$(".stratCountBtn").css({"cursor":"no-drop","opacity":".6"}).removeClass("countBtn stratHover");
		}
	})
	
	/*算一算的提交按钮*/
	$(".stratCountBtn").click(function(){

		if($(this).hasClass("countBtn")){

			var am = chagecity();

			inputResult(am,amzCommissionRate,txtCost,txtPrice,txtLength,txtWidth,txtHeight,txtWeightAcutal,txtFreightCost,txtRate,txtInRMB,txtMLL);

		}
	})
		
	/*汇率输入框*/
	$("#txtRate").on("keyup",function(){
		var am = chagecity();
		if(inputEmpty() == true){
			inputResult(am,amzCommissionRate,txtCost,txtPrice,txtLength,txtWidth,txtHeight,txtWeightAcutal,txtFreightCost,txtRate,txtInRMB,txtMLL);
		}
	})

	/*判断输入框不能为空*/
	function inputEmpty(){
		 amzCommissionRate =($("#amzCommissionRate").val() == "") ? "" : ($("#amzCommissionRate").val()).replace("%"," ")/100;
		 txtCost = ($("#txtCost").val() == "") ? "" : $("#txtCost").val();
		 txtPrice = ($("#txtPrice").val() == "") ? "" : $("#txtPrice").val();
		 txtLength = ($("#txtLength").val() == "") ? "" : $("#txtLength").val();
		 txtWidth = ($("#txtWidth").val() == "") ? "" : $("#txtWidth").val();
		 txtHeight = ($("#txtHeight").val() == "") ? "" : $("#txtHeight").val();
		 txtWeightAcutal = ($("#txtWeightAcutal").val() == "") ? "" : $("#txtWeightAcutal").val();
		 txtFreightCost = ($("#txtFreightCost").val() == "") ? "" : $("#txtFreightCost").val();
		 
		 txtRate = ($("#txtRate").val() == "") ? "" : $("#txtRate").val();
		if(txtCost != "" || (txtCost != "" && txtPrice != "") || (txtLength != "" && txtWidth != "" && txtHeight != "" && txtWeightAcutal != "") || (amzCommissionRate != "" && txtPrice != "") || txtRate != ""){
			return true;
		}else{
			return false;
		}
		
	}

	function inputResult(am,amzCommissionRate,txtCost,txtPrice,txtLength,txtWidth,txtHeight,txtWeightAcutal,txtFreightCost,txtRate,txtInRMB,txtMLL){

		var data = "";

		data = {amzMarket:am,amzCommissionRate:amzCommissionRate,txtCost:txtCost,txtPrice:txtPrice,txtLength:txtLength,txtWidth:txtWidth,txtHeight:txtHeight,txtWeightAcutal:txtWeightAcutal,txtFreightCost:txtFreightCost,txtRate:txtRate,txtInRMB:txtInRMB,txtMLL:txtMLL};

		data = "data=" + JSON.stringify(data);

		resultF(data);
	}

	function resultF(data){
		var calcStandSize     = $("#calcStandSize");
		var calcCost          = $("#calcCost");
		var calcShipping      = $("#calcShipping");
		var calcCommission    = $("#calcCommission");
		var calcFBA           = $("#calcFBA");
		var calcPrice         = $("#calcPrice");
		var calcMargin        = $("#calcMargin");
		var calcMarginPercent = $("#calcMarginPercent");
		var calcStatus        = $("#calcStatus");

		//statr edit xiaojing 2016-06-01
		/*$.ajax({
		 type:"post",
		 dataType:"json",
		 data:data,
		 url:"http://"+keyUrl+"/api/commission_api.php",
		 //url:"http://"+keyUrl+"/api/commission_api.php",
		 success:function(msg){
		 if(msg.code == 200){
		 calcStandSize.html(msg.value.calcStandSize);
		 calcCost.html(msg.value.calcCost);
		 calcShipping.html(msg.value.calcShipping);
		 calcCommission.html(msg.value.calcCommission);
		 calcFBA.html(msg.value.calcFBA);
		 calcPrice.html(msg.value.calcPrice);
		 calcMargin.html(msg.value.calcMargin);
		 calcMarginPercent.html(msg.value.calcMarginPercent);
		 calcStatus.html(msg.value.calcStatus);
		 //moneyMsg.html(msg.value.calcCurrencyRate);

		 var result = JSON.stringify(msg);
		 $.cookie('msg',result);
		 }
		 },
		 error:function(){
		 alert("Error:ajax请求失败");
		 },
		 complete:function(){
		 $(".contextDisplay").css("display","block");
		 }
		 });*/
		$.post('index.php?s=/home/index/commissionApi',data,function(msg){
			if(msg.code == 200){
				calcStandSize.html(msg.value.calcStandSize);
				calcCost.html(msg.value.calcCost);
				calcShipping.html(msg.value.calcShipping);
				calcCommission.html(msg.value.calcCommission);
				calcFBA.html(msg.value.calcFBA);
				calcPrice.html(msg.value.calcPrice);
				calcMargin.html(msg.value.calcMargin);
				calcMarginPercent.html(msg.value.calcMarginPercent);
				calcStatus.html(msg.value.calcStatus);
				//moneyMsg.html(msg.value.calcCurrencyRate);
				var result = JSON.stringify(msg);
				$.cookie('msg',result);
				$(".contextDisplay").css("display","block");
			}
		},'json');
		//end edit xiaojing 2016-06-01
	}
		
	/*获取Amazon平台的值*/
	var radioBtn = $(".radioContainer").find(".radioMsg");

	radioBtn.on("click",function(){
		var Market = $(this).children(".rMsg").text();
		amzMarket = Market;
		var am = chagecity();
		if($(".countBtn").length > 0){
			if(inputEmpty() == true){
				inputResult(am,amzCommissionRate,txtCost,txtPrice,txtLength,txtWidth,txtHeight,txtWeightAcutal,txtFreightCost,txtRate,txtInRMB,txtMLL);
			}
		}
		
	})
	
	/*更新数据*/
	/*$("#refreshRate").on("click",function(){
		var data = {"type":"getcurrency"};
		data = "type="+JSON.stringify(data);
		resultF(data)
	})*/
	
	$(".submitBtn").on("click",function(){
		if($.cookie("msg") != null){
			$(".contrastP").css("display","block");
			/*设置缓存的有效时间*/
			var data = new Date();
			data.setTime(data.getTime()+(180*60*1000));
			/*缓存数据*/
			if($.cookie("_index") == null){
				_index++;
			}else{
				_index = parseInt($.cookie("_index"));
				_index++;
			}
			
			$.cookie('submitResult'+_index,$.cookie("msg"),{expires:data});
			$.cookie("_index",_index,{expires:data});
			
			$.cookie("modalValue"+_index,$(".modalValue").val(),{expires:data});
			
			$.cookie("amzMarketTxt"+_index,amzMarket,{expires:data});
			addTr(_index);
		}
		
	})
	
	function addTr(indexs){
		var _html = "";
		var msg1 = $.cookie('submitResult'+indexs);
		var msg2 = JSON.parse(msg1);
		if(msg1 != null){
			_html +='<tr class="trColor">';
			_html +='<td>'+$.cookie("modalValue"+indexs)+'</td>';
			_html +='<td>'+$.cookie('amzMarketTxt'+indexs)+'</td>';
			(msg2.value.calcCost == "") ? _html +='<td>'+msg2.value.calcCost+'</td>' : _html +='<td>¥'+msg2.value.calcCost+'</td>';
			(msg2.value.calcShipping == "") ? _html +='<td>'+msg2.value.calcShipping+'</td>' : _html +='<td>¥'+msg2.value.calcShipping+'</td>';
			(msg2.value.calcCommission == "") ? _html +='<td>'+msg2.value.calcCommission+'</td>' : _html +='<td>'+msg2.value.calcBtnCurrencySymbol+''+msg2.value.calcCommission+'</td>';
			(msg2.value.calcFBA == "") ? _html +='<td>'+msg2.value.calcFBA+'</td>' : _html +='<td>'+msg2.value.calcBtnCurrencySymbol+''+msg2.value.calcFBA+'</td>';	
			(msg2.value.calcPrice == "") ? _html +='<td>'+msg2.value.calcPrice+'</td>' : _html +='<td>'+msg2.value.calcBtnCurrencySymbol+''+msg2.value.calcPrice+'</td>';
			(msg2.value.calcMargin == "") ? _html +='<td>'+msg2.value.calcMargin+'</td>' : _html +='<td>'+msg2.value.calcBtnCurrencySymbol+''+msg2.value.calcMargin+'</td>';
			(msg2.value.calcMarginPercent == "") ? _html +='<td>'+msg2.value.calcMarginPercent+'</td>' : _html +='<td>'+msg2.value.calcMarginPercent+'%</td>';
			_html +='<td>'+msg2.value.calcStatus+'</td>';
			_html +='<td class="deleteHover"><a href="javascript:void(0)" onclick="deleteBtn(this)"><i class="ace-icon fa fa-trash-o bigger-130 deleteB"></i></a></td>';
			_html +='</tr>';
			$(".tbodyData").append(_html);
		}
		$(".contextResult").css("display","block");
	}
	
	/*清空输入框和计算结果的所有值*/
	$(".stratClearBtn").on("click",function(){
		$("#txtPrice,#txtCost,#txtLength,#txtWidth,#txtHeight,#txtWeightAcutal,#txtFreightCost").val(" ");
		$(".stratCountBtn").css({"opacity":".6","cursor":"no-drop"}).removeClass("countBtn stratHover");
		$(".contextDisplay").css("display","none");
	})
	
	/*计算结果 统一人民币check按钮*/
	$(".checkBtnClick").on("click",function(){
		var am = chagecity();
		if(!$(this).hasClass("checkSelect")){
			$(this).addClass("checkSelect");
			 txtInRMB = "RMB";
			if(inputEmpty() == true){
				inputResult(am,amzCommissionRate,txtCost,txtPrice,txtLength,txtWidth,txtHeight,txtWeightAcutal,txtFreightCost,txtRate,txtInRMB,txtMLL);
			}
		}else{
			txtInRMB = "";
			$(this).removeClass("checkSelect");
			if(inputEmpty() == true){
				inputResult(am,amzCommissionRate,txtCost,txtPrice,txtLength,txtWidth,txtHeight,txtWeightAcutal,txtFreightCost,txtRate,txtInRMB,txtMLL);
			}
		}
		
	})
	
	/*毛利率计算*/
	$(".cheackBtn").on("click",function(){
		var am = chagecity();
		var chebtn = $(this).attr("checked");
		if(typeof(chebtn) == "undefined"){
			txtMLL = "1";
			$(this).attr("checked","checked");
			$(".lebeibtn2").css("display","block");
			$(".lebeibtn1").css("display","none");
			inputResult(am,amzCommissionRate,txtCost,txtPrice,txtLength,txtWidth,txtHeight,txtWeightAcutal,txtFreightCost,txtRate,txtInRMB,txtMLL);
		}else{
			txtMLL = "";
			$(this).removeAttr("checked");
			$(".lebeibtn2").css("display","none");
			$(".lebeibtn1").css("display","block");
			inputResult(am,amzCommissionRate,txtCost,txtPrice,txtLength,txtWidth,txtHeight,txtWeightAcutal,txtFreightCost,txtRate,txtInRMB,txtMLL);
		}
	})
	
	function chagecity(){
		var am = "US";
				if(amzMarket == '美国亚马逊'){
					am = 'US';
				}else if(amzMarket == '英国亚马逊'){
					am = 'UK';
				}else if(amzMarket == '法国亚马逊'){
					am = 'FR';
				}else if(amzMarket == '德国亚马逊'){
					am = 'DE';
				}
				
				return am;
	}

	/*利润计算excel导出*/
	$(".tableSubmitBtn").on("click",function(){

		var tableTileArry = [];

		var tableBodyArry = [];

		var  tableArrys = [];

		$("#table thead tr th").each(function(){
			tableTileArry.push($(this).text())
		})

		$("#table tbody tr td").each(function(){
			tableBodyArry.push($(this).text());
		})
		var len = $("#table tbody tr").length;
		for(var i=0;i<len;i++){
			tableArrys.push(tableBodyArry.splice(i,10));
		}
		tableTileArry.pop();
		var datas= {headerTile:tableTileArry,headerbody:tableArrys};
		datas ="data="+JSON.stringify(datas);

		$.post('index.php?s=/home/index/exportCommissionData',datas,function(msg){
			window.location.href = msg.url;
		},'json');
	})
	
})
